﻿using ExpressesClass.DepponApi.Base;
using ExpressesClass.DepponApi.billApplyNotify.request;
using ExpressesClass.DepponApi.billApplyNotify.response;
using ExpressesClass.DepponApi.billCancelNotify.request;
using ExpressesClass.DepponApi.billCancelNotify.response;
using ExpressesClass.DepponApi.billQueryNotify.request;
using ExpressesClass.DepponApi.billQueryNotify.response;
using ExpressesClass.DepponApi.billTraceNotify.request;
using ExpressesClass.DepponApi.billTraceNotify.response;
using ExpressesClass.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class DepponExpress : IExpress
    {
        //德邦快递参数
        private static string appKey = "";
        private static string companyCode = "";
        private static string cutomerCode = "";
        private static string signCode = "";
        //测试
        //private static string appKey = "";
        //private static string companyCode = "";
        //private static string cutomerCode = "";
        //private static string signCode = "";
        public Kddh GetMailNo(Kddh kddh)
        {
            BillApplyNotifyReq billApplyNotifyReq = new BillApplyNotifyReq();

            billApplyNotifyReq.logisticID = signCode + DateTime.Now.ToString("yyyyMMddHHmmssfff");
            billApplyNotifyReq.companyCode = companyCode;
            billApplyNotifyReq.orderType = "2";
            billApplyNotifyReq.transportType = kddh.shipName == "标准快递" ? "PACKAGE" : "";
            billApplyNotifyReq.customerCode = cutomerCode;
            billApplyNotifyReq.gmtCommit = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            billApplyNotifyReq.payType = kddh.payMethod == "月结支付" ? "2" : "0";

            billApplyNotifyReq.sender.name = kddh.sendMan;
            billApplyNotifyReq.sender.mobile = kddh.sendManPhone;
            billApplyNotifyReq.sender.province = kddh.sendProvince;
            billApplyNotifyReq.sender.city = kddh.sendCity;
            billApplyNotifyReq.sender.county = kddh.sendCounty;
            billApplyNotifyReq.sender.address = kddh.sendManAddress;
            billApplyNotifyReq.sender.companyName = kddh.sendCompany;

            billApplyNotifyReq.receiver.name = kddh.receiveMan;
            billApplyNotifyReq.receiver.mobile = kddh.receiveManPhone;
            billApplyNotifyReq.receiver.province = kddh.receiveProvince;
            billApplyNotifyReq.receiver.city = kddh.receiveCity;
            billApplyNotifyReq.receiver.county = kddh.receiveCounty;
            billApplyNotifyReq.receiver.address = kddh.receiveManAddress;
            billApplyNotifyReq.receiver.companyName = kddh.receiveCompany;

            billApplyNotifyReq.packageInfo.cargoName = kddh.itemName;
            billApplyNotifyReq.packageInfo.totalNumber = int.Parse(kddh.itemCount);
            billApplyNotifyReq.packageInfo.totalWeight = double.Parse(kddh.itemWeight);
            billApplyNotifyReq.packageInfo.totalVolume = 0.01;
            billApplyNotifyReq.packageInfo.deliveryType = "2";
            billApplyNotifyReq.packageInfo.packageService = "纸";

            if (decimal.Parse(kddh.insuranceValue) > 0)
            {
                billApplyNotifyReq.addServices.backSignBill = "0";
                billApplyNotifyReq.addServices.insuranceValue = decimal.Parse(kddh.insuranceValue);
            }

            DepponClient depponClient = new DepponClient(appKey, companyCode);

            BillApplyNotifyRsp res = depponClient.execute<BillApplyNotifyRsp>(billApplyNotifyReq);

            if (res.result == "true")
            {
                Kddh k = new Kddh();
                k.orderId = res.logisticID;
                k.mailNo = res.parentMailNo != null ? res.parentMailNo : res.mailNo;
                k.mailno_zd = res.parentMailNo != null ? string.Join(",", res.mailNo.Split(',').Where(p => p != res.parentMailNo)) : "";
                k.sendMan = kddh.sendMan;
                k.sendManPhone = kddh.sendManPhone;
                k.sendManAddress = kddh.sendManAddress;
                k.sendPostcode = kddh.sendPostcode;
                k.sendProvince = kddh.sendProvince;
                k.sendCity = kddh.sendCity;
                k.sendCounty = kddh.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = kddh.receiveMan;
                k.receiveManPhone = kddh.receiveManPhone;
                k.receiveManAddress = kddh.receiveManAddress;
                k.receivePostcode = kddh.receivePostcode;
                k.receiveProvince = kddh.receiveProvince;
                k.receiveCity = kddh.receiveCity;
                k.receiveCounty = kddh.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.markDestination = res.arrivedOrgSimpleName;
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = kddh.itemName;
                k.itemWeight = kddh.itemWeight;
                k.itemCount = kddh.itemCount;
                k.remark = kddh.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.insuranceValue = kddh.insuranceValue;
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                k.payMethod = kddh.payMethod;
                k.isReturnNo = kddh.isReturnNo;
                return k;
            }
            else if (!string.IsNullOrEmpty(res.reason))
            {
                MessageBox.Show(res.reason, "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }

        public bool CancelMailNo(Kddh kddh)
        {
            DepponClient depponClient = new DepponClient(appKey, companyCode);

            BillCancelNotifyReq billCancelNotifyReq = new BillCancelNotifyReq();

            billCancelNotifyReq.logisticCompanyID = "DEPPON";
            billCancelNotifyReq.logisticID = kddh.orderId;
            billCancelNotifyReq.mailNo = kddh.mailNo;
            billCancelNotifyReq.cancelTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            billCancelNotifyReq.remark = "业务需要";

            BillCancelNotifyRsp res = depponClient.execute<BillCancelNotifyRsp>(billCancelNotifyReq);

            if (res.result == "true")
            {
                return true;
            }
            else if (!string.IsNullOrEmpty(res.reason))
            {
                MessageBox.Show(res.reason, "提示");
            }
            else
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单失败,请重试.", "提示");
            }
            return false;
        }

        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            DepponClient depponClient = new DepponClient(appKey, companyCode);

            BillTraceNotifyReq billTraceNotifyReq = new BillTraceNotifyReq();

            billTraceNotifyReq.mailNo = kddh.mailNo;

            BillTraceNotifyRsp res = depponClient.execute<BillTraceNotifyRsp>(billTraceNotifyReq);

            //BillQueryNotifyReq billQueryNotifyReq = new BillQueryNotifyReq();
            //billQueryNotifyReq.logisticCompanyID = "DEPPON";
            //billQueryNotifyReq.logisticID = kddh.orderId + "_" + kddh.mailNo;

            //BillQueryNotifyRsp res_query = depponClient.execute<BillQueryNotifyRsp>(billQueryNotifyReq);
            //string insuranceValue = "0";

            //if (res_query.result == "true" && res_query.responseParam != null)
            //{
            //    insuranceValue = res_query.responseParam.insurancePrice.ToString();
            //}

            if (res.result == "true")
            {
                List<TraceLog> log = new List<TraceLog>();

                if (res.responseParam != null && res.responseParam.trace_list != null)
                {
                    foreach (Route r in res.responseParam.trace_list)
                    {
                        TraceLog t = new TraceLog();
                        t.acceptAddress = r.site;
                        t.acceptTime = r.time;
                        t.scanType = r.status;
                        t.remark = r.description;
                        log.Add(t);
                    }
                }
                return log;
            }
            else if (!string.IsNullOrEmpty(res.reason))
            {
                MessageBox.Show(res.reason, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }

        public void GetUseNum()
        {
            MessageBox.Show("德邦快递是月结形式.", "提示");
        }
    }
}
